package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.entity.*;
import com.smartfactory.sail.entity.enums.StorageStatusEnum;
import com.smartfactory.sail.mapper.ItemInspectioncriteriaMapper;
import com.smartfactory.sail.service.*;
import com.smartfactory.sail.vo.storageinspection.AddStorageInRecordReq;
import com.smartfactory.sail.vo.storageinspection.ShowResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 入库检测记录 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-25
 */
@Api(tags = "检验结果")
@RestController
@RequestMapping("/storage-inspection-record")
public class StorageInspectionRecordController extends ControllerResponse {

    private IStorageInspectionRecordService iStorageInspectionRecordService;
    private IStorageRecordService iStorageRecordService;
    private IStorageCurrentInfoService iStorageCurrentInfoService;
    private IItemService iItemService;
    private ItemInspectioncriteriaMapper itemInspectioncriteriaMapper;
    private IStorageInspectionService iStorageInspectionService;

    public StorageInspectionRecordController(IStorageInspectionRecordService iStorageInspectionRecordService, IStorageRecordService iStorageRecordService, IStorageCurrentInfoService iStorageCurrentInfoService, IItemService iItemService, ItemInspectioncriteriaMapper itemInspectioncriteriaMapper, IStorageInspectionService iStorageInspectionService) {
        this.iStorageInspectionRecordService = iStorageInspectionRecordService;
        this.iStorageRecordService = iStorageRecordService;
        this.iStorageCurrentInfoService = iStorageCurrentInfoService;
        this.iItemService = iItemService;
        this.itemInspectioncriteriaMapper = itemInspectioncriteriaMapper;
        this.iStorageInspectionService = iStorageInspectionService;
    }

    @OperRecord(content = "入库检验",level = 1)
    @PostMapping("/markResult")
    @ApiOperation(value = "添加检验结果 ", notes = "添加检验结果 ")
    public ApiResponse<Object> add( @RequestBody AddStorageInRecordReq req) {
        List<StorageInspectionRecord> records = req.getStorageInspectionRecords();

        Long storageRecodeId = req.getStorageRecodeId();

        StorageInspection storageInspection = new StorageInspection();
        storageInspection.setStorageRecodeId(storageRecodeId);
        storageInspection.setDetermine(req.getDetermine());
        storageInspection.setRemark(req.getRemark());
        storageInspection.setImgs(req.getImgs());
        QueryWrapper<StorageInspection> storageInspectionQueryWrapper = new QueryWrapper<>();
        storageInspectionQueryWrapper.eq("storageRecodeId", storageRecodeId);
        if (iStorageInspectionService.count(storageInspectionQueryWrapper) > 0) {
            List<StorageInspection> list = iStorageInspectionService.list(storageInspectionQueryWrapper);
            StorageInspection storageInspection1 = list.get(0);
            BeanUtils.copyProperties(req, storageInspection1);
            iStorageInspectionService.updateById(storageInspection1);

            UpdateWrapper<StorageInspectionRecord> storageInspectionRecordUpdateWrapper = new UpdateWrapper<>();
            storageInspectionRecordUpdateWrapper.eq("storageRecodeId",storageRecodeId);
            iStorageInspectionRecordService.remove(storageInspectionRecordUpdateWrapper);

            List<StorageInspectionRecord> storageInspectionRecords = req.getStorageInspectionRecords();
            for (StorageInspectionRecord storageInspectionRecord : storageInspectionRecords) {
                storageInspectionRecord.setStorageRecodeId(storageRecodeId);
            }
            iStorageInspectionRecordService.saveBatch(storageInspectionRecords);

        } else {
            if (iStorageInspectionService.save(storageInspection)) {
                List<StorageInspectionRecord> storageInspectionRecords = req.getStorageInspectionRecords();
                for (StorageInspectionRecord storageInspectionRecord : storageInspectionRecords) {
                    storageInspectionRecord.setStorageRecodeId(storageRecodeId);
                }
                iStorageInspectionRecordService.saveBatch(storageInspectionRecords);
            }
        }


        QueryWrapper<StorageInspectionRecord> query = new QueryWrapper<>();

        query.eq("storageRecodeId", req.getStorageRecodeId());
        int count = iStorageInspectionRecordService.count(query);
        if (count > 0) {
            for (StorageInspectionRecord record : records) {
                UpdateWrapper<StorageInspectionRecord> update = new UpdateWrapper<>();
                update.eq("storageRecodeId", req.getStorageRecodeId());
                update.eq("inspectionCriteriaId", record.getInspectionCriteriaId());
                update.set("resultInfo", record.getResultInfo());
                update.set("determine", record.getDetermine());
                iStorageInspectionRecordService.update(update);
            }

        } else{
            iStorageInspectionRecordService.saveBatch(records);
        }
        //判断总结果是通过还是驳回
        if (1 == req.getDetermine()) {
            //storageRecord结果变成入库 .
            iStorageRecordService.updateStatus(StorageStatusEnum.RK, storageRecodeId);
            //判断这个入库是订单  还是 备料  加数字 .
            iStorageCurrentInfoService.updateSize(storageRecodeId,1);
        } else if (0 == req.getDetermine()) {
            //直接修改这个入库记录为  不通过
            iStorageRecordService.updateStatus(StorageStatusEnum.JCBGG, storageRecodeId);
        } else {
            return fail("请检查判断结果传参!");
        }
        return ok();
    }

    @ApiOperation(value ="展示待检验项目",notes = "展示待检验项目")
    @GetMapping("show/{storageRecodeId}")
    public ApiResponse<ShowResp> show(@PathVariable("storageRecodeId")Long storageRecodeId) {

        StorageRecord storageRecord = iStorageRecordService.getById(storageRecodeId);
        storageRecord.getSize();
        Long itemId = storageRecord.getItemId();
        Item byId = iItemService.getById(itemId);
        ShowResp showResp = new ShowResp();
        showResp.setItemId(itemId);
        showResp.setItemCode(byId.getCode());
        showResp.setItemName(byId.getName());
        showResp.setItemSize(storageRecord.getSize());

        QueryWrapper<ItemInspectioncriteria> query = new QueryWrapper<>();
        query.eq("itemId", itemId);
        List<ItemInspectioncriteria> itemInspectioncriteria = itemInspectioncriteriaMapper.selectList(query);
        showResp.setItemInspectioncriteria(itemInspectioncriteria);
        return ok(showResp);
    }



    @ApiOperation(value ="展示检验结果",notes = "展示检验结果")
    @GetMapping("showResult/{storageRecodeId}")
    public ApiResponse<AddStorageInRecordReq> showResult(@PathVariable("storageRecodeId")Long storageRecodeId) {

        AddStorageInRecordReq result = new AddStorageInRecordReq();
        QueryWrapper<StorageInspection> inQu = new QueryWrapper<>();
        inQu.eq("storageRecodeId", storageRecodeId);
        List<StorageInspection> list = iStorageInspectionService.list(inQu);
        if (list == null || list.size() == 0) {
            return fail("无数据");
        }

        StorageInspection storageInspection = list.get(0);

        QueryWrapper<StorageInspectionRecord> recordQu = new QueryWrapper<>();
        recordQu.eq("storageRecodeId", storageRecodeId);
        List<StorageInspectionRecord> listrecord = iStorageInspectionRecordService.list(recordQu);


        result.setStorageRecodeId(storageRecodeId);
        result.setDetermine(storageInspection.getDetermine());
        result.setRemark(storageInspection.getRemark());
        result.setImgs(storageInspection.getImgs());

        result.setStorageInspectionRecords(listrecord);

        return ok(result);

    }
}
